package com.wenhui.nzapp.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.NzDinnerQrcode;
import com.gzwh.weblib.bean.NzDinnerRegion;
import com.wenhui.nzapp.R;

import java.util.List;

public class DinnerQrAdapter extends RecyclerView.Adapter<DinnerQrAdapter.MyViewHolder> {
    private List<NzDinnerQrcode> datas;
    private LayoutInflater inflater;
    private int checked;
    private Context mContext;

    private OnUserClick mOnUserClick;

    public void setmOnUserClick(OnUserClick mOnUserClick) {
        this.mOnUserClick = mOnUserClick;
    }

    public interface OnUserClick {
        void onClick(NzDinnerQrcode dTreeCategoryModel);
    }

    public DinnerQrAdapter(Context context, List<NzDinnerQrcode> datas) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        this.datas = datas;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_type, null);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textview.setText(datas.get(position).getTableName());
        if (position == checked) {
            holder.itemMain.setBackgroundColor(Color.WHITE);
            holder.textview.setTextColor(Color.BLACK);
            holder.textview.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            holder.itemMain.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_color_blue_3_pressed));
            holder.textview.setTextColor(ContextCompat.getColor(mContext, R.color.type_normal));
            holder.textview.setTypeface(Typeface.DEFAULT);
        }
        holder.itemMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(position);
                if (mOnUserClick != null) {
                    mOnUserClick.onClick(datas.get(position));
                }
            }
        });
    }


    public void setChecked(int checked) {
        this.checked = checked;
        notifyDataSetChanged();
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textview;
        private RelativeLayout itemMain;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview = (TextView) itemView.findViewById(R.id.tv_name);
            itemMain = (RelativeLayout) itemView.findViewById(R.id.item_main);
        }
    }
}
